layui.config({
    base: "js/"
}).use(['form', 'layer', 'jquery', 'laypage'], function () {
    var form = layui.form(),
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        laypage = layui.laypage,
        $ = layui.jquery;

    //加载页面数据
    var menusData = '';
    $.get("http://localhost/ci/admin/menu/imp_index", {'token': localStorage.token}, function (data) {
        /*if(data == '0'){
         location.href = 'http://localhost/ci/login/login/login';
         }*/
        menusData = JSON.parse(data);
        //执行加载数据的方法
        menusList();
    })


    //全选
    form.on('checkbox(allChoose)', function (data) {
        var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
        child.each(function (index, item) {
            item.checked = data.elem.checked;
        });
        form.render('checkbox');
    });

    //通过判断文章是否全部选中来确定全选按钮是否选中
    form.on("checkbox(choose)", function (data) {
        var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
        var childChecked = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"]):checked')
        if (childChecked.length == child.length) {
            $(data.elem).parents('table').find('thead input#allChoose').get(0).checked = true;
        } else {
            $(data.elem).parents('table').find('thead input#allChoose').get(0).checked = false;
        }
        form.render('checkbox');
    })

    function menusList() {
        //渲染数据
        function renderDate(data, curr) {
            var dataHtml = '';
            currData = menusData.concat().splice(curr * nums - nums, nums);
            var i = 0;
            if(curr == 1){
                i = 1;
            }
            if (currData.length != 0) {
                for (; i < currData.length; i++) {
                    dataHtml += '<tr>'
                        + '<td>' + currData[i].name + '</td>'
                        + '<td>' + currData[i].url + '</td>'
                        + '<td>' + currData[i].perms + '</td>'
                        + '<td>' + currData[i].type + '</td>'
                        + '<td>'+'<i class="layui-icon" style="font-size: 30px;">'+ currData[i].icon +'</i>'+ '</td>'
                        + '<td>'
                        + '<a class="layui-btn layui-btn-mini menu_edit" data-id="' + currData[i].menu_id + '"><i class="iconfont icon-edit"></i> 编辑</a>'
                        + '</td>'
                        + '</tr>';
                }
            } else {
                dataHtml = '<tr><td colspan="8">暂无数据</td></tr>';
            }
            return dataHtml;
        }

        //分页
        var nums = 8; //每页出现的数据量
        laypage({
            cont: "page",
            pages: Math.ceil(menusData.length / nums),
            jump: function (obj) {
                $(".menus_content").html(renderDate(menusData, obj.curr));
                $('.menus_list thead input[type="checkbox"]').prop("checked", false);
                form.render();
            }
        })
    }

//--------------弹出页面------------------
    //操作
    $("body").on("click", ".menu_edit", function () {  //编辑
        var _this = $(this);
        $('.ci_data').val(_this.attr('data-id'));
        var index = layui.layer.open({
            title: "修改角色",
            type: 2,
            content: "http://localhost/ci/admin/menu/updateMenu",
            success: function (layero, index) {
                layui.layer.tips('点击此处返回文章列表', '.layui-layer-setwin .layui-layer-close', {
                    tips: 3
                });
            }
        })
        //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
        $(window).resize(function () {
            layui.layer.full(index);
        })
        layui.layer.full(index);
    })

    //添加菜单
    $("body").on("click", ".menu_add", function () {
        var _this = $(this);
        $('.ci_data').val(_this.attr('data-id'));
        var index = layui.layer.open({
            title: "添加角色",
            type: 2,
            content: "http://localhost/ci/admin/menu/addMenu",
            success: function (layero, index) {
                layui.layer.tips('点击此处返回文章列表', '.layui-layer-setwin .layui-layer-close', {
                    tips: 3
                });
            }
        })
        //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
        $(window).resize(function () {
            layui.layer.full(index);
        })
        layui.layer.full(index);
    })

    $(".menu_del").click(function () {

        var index = layui.layer.open({
            title: "删除菜单",
            type: 2,
            area: ['600px', '360px'],
            maxmin: true,
            shadeClose: true, //点击遮罩关闭层
            content: "http://localhost/ci/admin/menu/delete",
            success: function (layero, index) {
                layui.layer.tips('点击此处返回文章列表', '.layui-layer-setwin .layui-layer-close', {
                    tips: 3
                });
            }
        })
    })

})
